package com.xjrccb.meeting.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.xjrccb.meeting.bean.Department;
import com.xjrccb.meeting.bean.Meeting;
import com.xjrccb.meeting.bean.User;
import com.xjrccb.meeting.dao.AddMeetingDao;

public class DeviceController implements Controller {
	@Autowired
	private AddMeetingDao add ;
	@Override
	public ModelAndView handleRequest(HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("userList",getAllUser());
		mv.addObject("message", getGroupJson());
		mv.setViewName("Device");
		return mv;
	}
	
	private String getAllUser(){
		List<User> listUser = add.queryUser();
		String ini ="";
		JSONObject objRes = new JSONObject();
		JSONArray everyArray = new JSONArray() ;
		for (User user :listUser){
			JSONObject obj = new JSONObject();
			obj.put("id", user.getId());
			obj.put("realName", user.getRealName());
			obj.put("userName", user.getUserName());
			obj.put("uuid", user.getUuid());
			obj.put("deptName", user.getDeptName());
			if(ini.equals(user.getInitial())){
				everyArray.put(obj);
			}else{
				if(!"".equals(ini)){
					objRes.put(ini,everyArray);
				} 
				everyArray = new JSONArray() ;
				everyArray.put(obj);
				
//				if("".equals(ini)){
//					JSONObject obj = new JSONObject();
//					obj.put("id", user.getId());
//					obj.put("realName", user.getRealName());
//					obj.put("userName", user.getUserName());
//					obj.put("uuid", user.getUuid());
//					everyArray.put(obj);
//					objRes.put(user.getInitial(),everyArray);
//				}else{
//					objRes.put(user.getInitial(),everyArray);
//					everyArray = new JSONArray();
//					JSONObject obj = new JSONObject();
//					obj.put("id", user.getId());
//					obj.put("realName", user.getRealName());
//					obj.put("userName", user.getUserName());
//					obj.put("uuid", user.getUuid());
//					everyArray.put(obj);
//				}
				ini=user.getInitial();
			}
		}
		objRes.put(ini,everyArray);
		System.out.println(objRes.toString());
		return objRes.toString();
	}
	
	private String getGroupJson(){
		List<Department> listDept = add.queryDept();
		List<User> listDeptUser;
		JSONArray resultArray = new JSONArray();
		JSONObject obj = new JSONObject();
//		obj.put("childList", me.getId());
		for(Department me :listDept){
	 		JSONObject obj1 = new JSONObject(); 
	 		obj1.put("id", me.getId());
	 		obj1.put("name", me.getName());
	 		obj1.put("parentId", me.getParentId());
	 		obj1.put("parentIds", me.getParentIds());
	 		obj1.put("childList","");
	 		listDeptUser = add.queryUserByDept(me.getId());
	 		JSONArray userArr = new JSONArray();
	 		for(User user :listDeptUser){
	 			JSONObject obj2 = new JSONObject(); 
	 			obj2.put("id", user.getId());
	 			obj2.put("realName", user.getRealName());
	 			obj2.put("userName", user.getUserName());
	 			obj2.put("uuid", user.getUuid());
	 			userArr.put(obj2);
	 		}
	 		obj1.put("userList", userArr);
	 		resultArray.put(obj1);
	 	}
		return resultArray.toString();
	}
}
